
package com.springboot.rmq.comm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;


public enum Selector implements MessageQueueSelector {

    INSTANCE;

    @Override
    public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
        Log log = LogFactory.getLog("MessageQueueSelector");
        log.info(">>>>>>>>>>>>>>>>>>>> " + "Selector.key            = " + String.valueOf(o));
        if (o == null) {
            o = message;
            log.info(">>>>>>>>>>>>>>>>>>>> " + "message.hashCode()      = " + o.hashCode());
        } else {
            log.info(">>>>>>>>>>>>>>>>>>>> " + "Selector.key.hashCode() = " + o.hashCode());
        }

        int index = Math.abs(o.hashCode()) % list.size();

        if (o instanceof Integer) {
            index = ((int) o) % list.size();
        }
        log.info(">>>>>>>>>>>>>>>>>>>> " + "MessageQueue.size()     = " + list.size() );
        log.info(">>>>>>>>>>>>>>>>>>>> " + "MessageQueue.index      = " + index);
        MessageQueue messageQueue = list.get(index);
        log.info(">>>>>>>>>>>>>>>>>>>> 选中队列：topic=" + messageQueue.getTopic()
                + ", brokerName=" + messageQueue.getBrokerName()
                + ", queueId=" + messageQueue.getQueueId());
        return messageQueue;
    }
}
